#!/bin/bash
# General Puppi functions

BOOTUP=color
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[0;32m"
SETCOLOR_FAILURE="echo -en \\033[0;31m"
SETCOLOR_WARNING="echo -en \\033[0;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"
SETCOLOR_TITLE="echo -en \\033[0;35m"
SETCOLOR_BOLD="echo -en \\033[0;1m"

echo_success() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $"  OK  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 0
}

echo_dontdeploy() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $" NO NEED TO DEPLOY "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 0
}

echo_failure() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo -n $"FAILED"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}

echo_passed() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo -n $"PASSED"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}

echo_warning() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo -n $"WARNING"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}

echo_title () {
  echo
  echo
  [ "$BOOTUP" = "color" ] && $SETCOLOR_TITLE
  echo "$1"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
}

check_retcode () {
    if [ $? = "0" ] ; then
        true
    else
        exit 2
    fi
}

handle_result () {
        RETVAL=$?
        if [ "$RETVAL" = "0" ] ; then
            showresult="echo_success"
            result="OK"
        fi
        if [ "$RETVAL" = "1" ] ; then
            showresult="echo_warning"
            EXITWARN="1"
            result="WARNING"
        fi
        if [ "$RETVAL" = "2" ] ; then
            showresult="echo_failure"
            EXITCRIT="1"
            result="CRITICAL"
        fi
        if [ "$RETVAL" = "99" ] ; then
            showresult="echo_dontdeploy"
            DONTDEPLOY="1"
            result="OK"
        fi
        if [ x$show == "xyes" ] ; then
            $showresult
            echo
            echo -e "$output"
            echo
        elif [ x$show == "xfail" ] && [ x$RETVAL != "x0" ] ; then
            $showresult
            echo
            echo -e "$output"
            echo
        fi

        # Output to file
        if [ ! -d $logdir/$project/$tag ] ; then
            mkdir -p $logdir/$project/$tag
        fi
        let counter=counter+1
        echo $title > $logdir/$project/$tag/$counter-$command
        echo $code >> $logdir/$project/$tag/$counter-$command
        echo $result >> $logdir/$project/$tag/$counter-$command
        echo $output >> $logdir/$project/$tag/$counter-$command

}


# Function taken from http://www.threadstates.com/articles/parsing_xml_in_bash.html
xml_parse () {
    local tag=$1
    local xml=$2

    # Find tag in the xml, convert tabs to spaces, remove leading spaces, remove the tag.
    grep $tag $xml | \
        tr '\011' '\040' | \
        sed -e 's/^[ ]*//' \
            -e 's/^<.*>\([^<].*\)<.*>$/\1/'
}

# Stores the passed arguments in Project runtime config file
# Only if the parameter is not already defined
# Usage:
# save_runtime_config parameter=value # Sets or overrides parameter
# save_runtime_config parameter=value notforce # Sets parameters only if is not already set
save_runtime_config () {
    parameter=$(echo $1 | cut -d '=' -f1)
    value=$(echo $1 | cut -d '=' -f2-)
    force=$2

    if [[ ! $(grep $parameter $workdir/$project/config) ]] ; then
        echo  >> $workdir/$project/config
        echo "# Added by $0" >> $workdir/$project/config
        echo "$parameter=\"$value\"" >> $workdir/$project/config
    else
#        sed -i "/^$parameter=/d" $workdir/$project/config # No real need to remove lines with old configs
        if [[ x$force == xnotforce ]] ; then
            echo  >> $workdir/$project/config
            echo "# CHANGE NOT FORCED by $0" >> $workdir/$project/config
            echo "# $parameter=\"$value\"" >> $workdir/$project/config
        else
            echo  >> $workdir/$project/config
            echo "# CHANGED by $0" >> $workdir/$project/config
            echo "$parameter=\"$value\"" >> $workdir/$project/config
       fi
    fi
    
}

# Adds a runtime comment to Project runtime config file
save_runtime_comment () {
    echo  >> $workdir/$project/config
    echo "# Added by $0" >> $workdir/$project/config
    echo "  ## $1" >> $workdir/$project/config
}


# Stores the passed arguments in Project runtime config file
# Forces parameter overwrite if already defined
overwrite_runtime_config () {
    echo "$1" >> $workdir/$project/config
}

ask_interactive () {
    if [ x$show == "xyes" ] ; then
        echo -n $title
    fi

    if [ "$interactive" = "yes" ] ; then
        echo 
        echo "INTERACTIVE MODE: Press 'x' to exit or just return to go on" 
        read press
        case $press in 
            x) exit 2 ;;
            *) return
        esac
    fi
}

# Shows or executes a command
show_command () {
   echo
   $SETCOLOR_BOLD ; echo "$HOSTNAME: $*" ; $SETCOLOR_NORMAL

   bash -c "$*"

# Grep filter at show_command level
#   if [ ! -z "$greppattern" ] ; then
#       bash -c "$*" | grep $greppattern
#   else
#       bash -c "$*"
#   fi
}

# Filtering out only:  $ ; ` | < >
shell_filter () {
    echo $1 | sed 's/\$//g' | sed 's/;//g' | sed 's/`//g' | sed 's/|//g' | sed 's/<//g' | sed 's/>//g'
}

shell_filter_strict () {
# Filtering out:  $ ; ` | < > = ! { } [ ] / \ # &
    echo $1 | sed 's/\$//g' | sed 's/;//g' | sed 's/`//g' | sed 's/|//g' | sed 's/<//g' | sed 's/>//g'  | sed 's/=//g' | sed 's/!//g' | sed 's/{//g' | sed 's/}//g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/\///g' | sed 's/\\//g' | sed 's/#//g' | sed 's/&//g'

# Filtering out: all but accepted chars
#     echo $1 | sed "s/[^a-Z0-9_\-]//Ig"
}

